<?php

namespace app\admin\controller;

use app\admin\model\FileUploadConfig;
use app\admin\service\FileUploadConfigService;
use app\admin\validate\FileConfigValidate;
use common\enum\ResultCode;
use hg\apidoc\annotation as Apidoc;
use support\Request;
use support\Response;
use support\Container;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;

#[Apidoc\Title("文件配置管理")]
#[Apidoc\Sort(999)]
class FileUploadConfigController extends BaseController
{
    private FileUploadConfigService $service;
    protected FileConfigValidate $validate;

    public function __construct()
    {
        $this->service = Container::get(FileUploadConfigService::class);
        $this->validate = Container::get(FileConfigValidate::class);
    }

    #[Apidoc\Title("配置详情")]
    #[Apidoc\Url("admin/fileConfig/info")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query("id", type: "int", require: true, desc: "配置ID")]
    #[Apidoc\Returned(type: "object", ref: FileUploadConfig::class, desc: "配置详情")]
    public function info(Request $request): Response
    {
        $id = $request->get('id');
        if (!$this->validate->check(['id' => $id], 'info')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getInfo($id);
        return $this->success($result);
    }

    #[Apidoc\Title("更新配置")]
    #[Apidoc\Url("admin/fileConfig/update")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "配置ID")]
    #[Apidoc\Param("config", type: "object", require: false, desc: "存储类型特定配置")]
    #[Apidoc\Param("single_limit", type: "int", require: false, desc: "单个文件大小限制(字节)")]
    #[Apidoc\Param("total_limit", type: "int", require: false, desc: "总文件大小限制(字节)")]
    #[Apidoc\Param("nums", type: "int", require: false, desc: "文件数量限制")]
    #[Apidoc\Param("include", type: "string", require: false, desc: "允许的文件类型")]
    #[Apidoc\Param("exclude", type: "string", require: false, desc: "不允许的文件类型")]
    #[Apidoc\Param("remark", type: "string", require: false, desc: "备注")]
    #[Apidoc\Param("status", type: "int", require: true, desc: "状态:0=禁用,1=启用")]
    #[Apidoc\Param("sort", type: "int", require: true, desc: "排序")]
    #[Apidoc\Returned(type: "object", ref: FileUploadConfig::class, desc: "配置信息")]
    public function update(Request $request): Response
    {
        $id = $request->post('id');
        $data = [
            'config' => $request->post('config'),
            'single_limit' => $request->post('single_limit'),
            'total_limit' => $request->post('total_limit'),
            'nums' => $request->post('nums'),
            'include' => $request->post('include'),
            'exclude' => $request->post('exclude'),
            'remark' => $request->post('remark'),
            'status' => $request->post('status'),
            'sort' => $request->post('sort'),
        ];
        $data = array_filter(array_merge(['id' => $id], $data), function ($value) {
            return !empty($value) || $value === 0;
        });
        if (!$this->validate->check($data, 'update')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->update($id, $data);
        return $this->success($result, ResultCode::UPDATE_SUCCESS->getDescription());
    }
}